﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CRM_SMS_MARKETING.EL;
using System.Data;
using ConnectDatabase;
using System.Configuration;
using GSM.BusinessLayer.DataLayer;

namespace CRM_SMS_MARKETING.DAL
{
    class DALCustomer
    {

        #region Customer
        ConnectDB con;

        public List<ELCustomer> getCustomer()
        {
            try
            {
                
                List<ELCustomer> ListCustomer = new List<ELCustomer>();
                con = new ConnectDB(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
                DataTable cusTab = con.getTable("SELECT * FROM CUSTOMER");
                ELCustomer customer;
                foreach (DataRow i in cusTab.Rows)
                {
                    customer = new ELCustomer();

                    #region mapping
                    customer.CustomerID = (int)i["CustomerID"];
                    customer.FirstName = i["FirstName"].ToString();
                    customer.LastName = i["LastName"].ToString();
                    customer.CellPhone = i["CellPhone"].ToString();
                    customer.Gender = (bool)i["Gender"];
                    customer.Birthday = (DateTime)i["Birthday"];
                    customer.Addrress = i["Addrress"].ToString();
                    customer.District = i["District"].ToString();
                    customer.City = i["City"].ToString();
                    customer.Phone = i["Phone"].ToString();
                    customer.Company = i["Company"].ToString();
                    customer.Email = i["Email"].ToString();
                    customer.User = i["User"].ToString();
                    #endregion

                    //addCustomer(customer);
                    ListCustomer.Add(customer);
                }

                return ListCustomer;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void addCustomer(ELCustomer customer)
        {
            try
            {
                con.executeNonQuery("INSERT INTO CUSTOMER (FirstName, LastName, CellPhone, Gender, Birthday, Addrress, District, "
                    + "City, Phone, Company, Email, [User])"
                    + "VALUES (N'" + customer.FirstName + "', N'" + customer.LastName + "', '" + customer.CellPhone + "', '" + customer.Gender + "', '" + customer.Birthday + "'"
                    + ", N'" + customer.Addrress + "', N'" + customer.District + "', N'" + customer.City + "', '" + customer.Phone + "', N'" + customer.Company + "', '" + customer.Email + "', '" + customer.User + "')");
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
   
        }
        public ELCustomer GetACustomerByID(string ID)
        {
            ELCustomer customer = new ELCustomer();
            con = new ConnectDB("Data Source=THE-VAIO\\DDNI;Initial Catalog=CRM_SMS;Integrated Security=True");
            DataTable cusTab = con.getTable("SELECT * FROM CUSTOMER WHERE CustomerID='"+ID+"'");
            #region mapping
            customer.CustomerID = (int)cusTab.Rows[0]["CustomerID"];
            customer.FirstName = cusTab.Rows[0]["FirstName"].ToString();
            customer.LastName = cusTab.Rows[0]["LastName"].ToString();
            customer.CellPhone = cusTab.Rows[0]["CellPhone"].ToString();
            customer.Gender = (bool)cusTab.Rows[0]["Gender"];
            customer.Birthday = (DateTime)cusTab.Rows[0]["Birthday"];
            customer.Addrress = cusTab.Rows[0]["Addrress"].ToString();
            customer.District = cusTab.Rows[0]["District"].ToString();
            customer.City = cusTab.Rows[0]["City"].ToString();
            customer.Phone = cusTab.Rows[0]["Phone"].ToString();
            customer.Company = cusTab.Rows[0]["Company"].ToString();
            customer.Email = cusTab.Rows[0]["Email"].ToString();
            customer.User = cusTab.Rows[0]["User"].ToString();
            #endregion
            return customer;
        }
        public void Update(ELCustomer customer)
        {
            try
            {
                con.executeNonQuery("UPDATE CUSTOMER SET (FirstName, LastName, CellPhone, Gender, Birthday, Addrress, District, "
                    + "City, Phone, Company, Email, [User])"
                    + "VALUES (N'" + customer.FirstName + "', N'" + customer.LastName + "', '" + customer.CellPhone + "', '" + customer.Gender + "', '" + customer.Birthday + "'"
                    + ", N'" + customer.Addrress + "', N'" + customer.District + "', N'" + customer.City + "', '" + customer.Phone + "', N'" + customer.Company + "', '" + customer.Email + "', '" + customer.User + "')");

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        public void GetLastID()
        {
            
        }
    }
}
